package com.chatify.me;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
 
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

public class NetworkConnection{
	
	//private XMPPClient xmppClient;
	public static XMPPConnection xmppConnection;
	public Handler mHandler = new Handler();
	public static ConnectionModel connectionConfig = null;
	 
	public NetworkConnection(ConnectionModel config )
	{
		this(config, false);
	}
	
	public NetworkConnection(ConnectionModel config, boolean openConnection){
		 
		NetworkConnection.connectionConfig = config;
		if ( openConnection)
		{
			// Create a connection
			 ConnectionConfiguration connectioncnfg =
			            new ConnectionConfiguration(config.hostName, 
			            		config.portNumber,
			            		config.serviceName);
			 connectioncnfg.setCompressionEnabled(false);
			 connectioncnfg.setSASLAuthenticationEnabled(false);
			    xmppConnection = new XMPPConnection(connectioncnfg);
			   
			    try {
			    	xmppConnection.connect();
			        Log.i("XMPPClient", "[SettingsDialog] Connected to " + xmppConnection.getHost());
			    } catch (XMPPException ex) {
			        Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + xmppConnection.getHost());
			        Log.e("XMPPClient", ex.toString());
			        xmppConnection=null;
			        
			    }
			    try {
			    	
			    	xmppConnection.login(config.userName,config.password);
			    	
			        Log.i("XMPPClient", "Logged in as " + xmppConnection.getUser());

			        // Set the status to available
			        Presence presence = new Presence(Presence.Type.available);
			        xmppConnection.sendPacket(presence);
			     
			    } catch (XMPPException ex) {
			        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + config.userName);
			        Log.e("XMPPClient", ex.toString());
			        xmppConnection=null;
			        
			    }
			
			//new ConnectAsyncAction().execute();
		}
		else{
			xmppConnection=null;
		}
		
	}
	
	final class ConnectAsyncAction extends AsyncTask<Void, Void, Void> {
		@Override
	    protected Void doInBackground(Void... args) { 
			 Log.i("I am in ","i");
			 
			ConnectionModel config= NetworkConnection.connectionConfig;
			// Create a connection
			 ConnectionConfiguration connectioncnfg =
			            new ConnectionConfiguration(config.hostName, 
			            		config.portNumber,
			            		config.serviceName);
			 connectioncnfg.setCompressionEnabled(false);
			 connectioncnfg.setSASLAuthenticationEnabled(false);
			    xmppConnection = new XMPPConnection(connectioncnfg);
			   
			    try {
			    	xmppConnection.connect();
			        Log.i("XMPPClient", "[SettingsDialog] Connected to " + xmppConnection.getHost());
			    } catch (XMPPException ex) {
			        Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + xmppConnection.getHost());
			        Log.e("XMPPClient", ex.toString());
			        xmppConnection=null;
			        
			    }
			    try {
			    	
			    	xmppConnection.login(config.userName,config.password);
			    	
			        Log.i("XMPPClient", "Logged in as " + xmppConnection.getUser());

			        // Set the status to available
			        Presence presence = new Presence(Presence.Type.available);
			        xmppConnection.sendPacket(presence);
			     
			    } catch (XMPPException ex) {
			        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + config.userName);
			        Log.e("XMPPClient", ex.toString());
			        xmppConnection=null;
			        
			    }
			 
	    
			return null;
	    }
 

	    // This is called each time you call publishProgress()
		@Override
		protected void onProgressUpdate(Void... progress) {
	       
	    }

	    // This is called when doInBackground() is finished
		@Override
		protected void onPostExecute(Void result) {
        
    }
	}
}

